package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.MmrbT;
import cn.e4j.modular.crm.mapper.MmrbTMapper;
import cn.e4j.modular.crm.service.MmrbTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-01-16
 */
@Service
public class MmrbTServiceImpl extends ServiceImpl<MmrbTMapper, MmrbT> implements MmrbTService {
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {

        //List<Map<String, Object>> mmrbDtos = this.baseMapper.listPage(page, dto);
        List<Map<String, Object>> mmrbDtos = InvokeUtil.invokeBeanMethod("mmrbTMapper,listPage",page,dto);
        return mmrbDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwakList(Dto dto) {

        List<Map<String, Object>> gwakDtos = this.baseMapper.getGwakList(dto);

        return gwakDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMmraList(Dto dto) {

        List<Map<String, Object>> mmraDtos = this.baseMapper.getMmraList(dto);

        return mmraDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwamList(Dto dto) {

        List<Map<String, Object>> gwamDtos = this.baseMapper.getGwamList(dto);

        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public MmrbT getByKey(String MMRBENT, String MMRB001, String MMRB002, String MMRB003, String MMRB004, String MMRB005,
                          String MMRB016, String MMRB017, Double MMRB024) {
        MmrbT mmrb = this.baseMapper.getByKey(MMRBENT, MMRB001, MMRB002, MMRB003, MMRB004, MMRB005, MMRB016, MMRB017,
                MMRB024);
        return mmrb;
    }

    @Transactional
    @Override
    public void add(MmrbT mmrbT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        double mmrb015 = 1;
        double mmrb024 = 0;
        mmrbT.setMMRB015(mmrb015);
        mmrbT.setMMRB016(" ");
        mmrbT.setMMRB017(" ");
        mmrbT.setMMRB024(mmrb024);
        mmrbT.setMMRB010(mmrbT.getMMRB010());
        mmrbT.setMMRB011(mmrbT.getMMRB011());
        mmrbT.setMMRBENT(shiroUser.getDBC());
        mmrbT.setMMRBUSER(shiroUser.getACCOUNT());
        mmrbT.setMMRBGRUP(shiroUser.getORG_ID());
        mmrbT.setMMRBDATE(LocalDate.now());
        mmrbT.setMMRBTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        mmrbT.setMMRBORIG(shiroUser.getORG_ID());
        mmrbT.setMMRBORIU(shiroUser.getACCOUNT());
        mmrbT.insert();
    }

    @Transactional
    @Override
    public void edit(MmrbT mmrb) {
        ShiroUser shiroUser = ShiroKit.getUser();
        mmrb.setMMRBMODU(shiroUser.getACCOUNT());
        UpdateWrapper<MmrbT> mmrbTUpdateWrapper = new UpdateWrapper<>();
        mmrbTUpdateWrapper.eq("MMRBENT", mmrb.getMMRBENT());
        mmrbTUpdateWrapper.eq("MMRB001", mmrb.getMMRB001());
        mmrbTUpdateWrapper.eq("MMRB002", mmrb.getMMRB002());
        mmrbTUpdateWrapper.eq("MMRB003", mmrb.getMMRB003());
        mmrbTUpdateWrapper.eq("MMRB004", mmrb.getMMRB004());
        mmrbTUpdateWrapper.eq("MMRB005", mmrb.getMMRB005());
        mmrbTUpdateWrapper.eq("MMRB016", mmrb.getMMRB016());
        mmrbTUpdateWrapper.eq("MMRB017", mmrb.getMMRB017());
        mmrbTUpdateWrapper.eq("MMRB024", mmrb.getMMRB024());
        mmrb.update(mmrbTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delete(MmrbT mmrbT) {
        UpdateWrapper<MmrbT> mmrbTUpdateWrapper = new UpdateWrapper<>();
        mmrbTUpdateWrapper.eq("MMRBENT", mmrbT.getMMRBENT());
        mmrbTUpdateWrapper.eq("MMRB001", mmrbT.getMMRB001());
        mmrbTUpdateWrapper.eq("MMRB002", mmrbT.getMMRB002());
        mmrbTUpdateWrapper.eq("MMRB003", mmrbT.getMMRB003());
        mmrbTUpdateWrapper.eq("MMRB004", mmrbT.getMMRB004());
        mmrbTUpdateWrapper.eq("MMRB005", mmrbT.getMMRB005());
        mmrbTUpdateWrapper.eq("MMRB016", mmrbT.getMMRB016());
        mmrbTUpdateWrapper.eq("MMRB017", mmrbT.getMMRB017());
        mmrbTUpdateWrapper.eq("MMRB024", mmrbT.getMMRB024());
        mmrbT.delete(mmrbTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<MmrbT> mmrbTList) {
        for (MmrbT mmrbT : mmrbTList) {
            UpdateWrapper<MmrbT> mmrbTUpdateWrapper = new UpdateWrapper<>();
            mmrbTUpdateWrapper.eq("MMRBENT", mmrbT.getMMRBENT());
            mmrbTUpdateWrapper.eq("MMRB001", mmrbT.getMMRB001());
            mmrbTUpdateWrapper.eq("MMRB002", mmrbT.getMMRB002());
            mmrbTUpdateWrapper.eq("MMRB003", mmrbT.getMMRB003());
            mmrbTUpdateWrapper.eq("MMRB004", mmrbT.getMMRB004());
            mmrbTUpdateWrapper.eq("MMRB005", mmrbT.getMMRB005());
            mmrbTUpdateWrapper.eq("MMRB016", mmrbT.getMMRB016());
            mmrbTUpdateWrapper.eq("MMRB017", mmrbT.getMMRB017());
            mmrbTUpdateWrapper.eq("MMRB024", mmrbT.getMMRB024());
            mmrbT.delete(mmrbTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMmraList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> mmraDtos = this.baseMapper.listMmraPage(page, dto);
        return mmraDtos;
    }
}
